package com.localjoycommunity.cpdj.common;

import java.io.Serializable;

/**
 * @Function:
 * @Author: aqie
 * @Date: 2019/4/5 11:44
 */
public class R<M> implements Serializable {
    private int code;
    private String msg;

    private M data;

    public R() {
        code = 200;
        msg = "ok";
    }

    public R(String msg){
        code = 1;
        this.msg = msg;
    }

    public R(M result) {
        this();
        this.data = result;
    }

    public static <M> R<M> success() {
        return new R<M>();
    }


    // 成功时调用
    public static <M> R<M> success(M data) {
        return new R<M>(data);
    }

    public static <M> R<M> error(String msg){
        return new R<>(msg);
    }
    public static <M> R<M> error(CodeMsg codeMsg){
        return new R<M>(codeMsg);
    }

    private R(CodeMsg codeMsg){
        if (codeMsg == null) {
            return;
        }
        this.code = codeMsg.getCode();
        this.msg = codeMsg.getMsg();
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public M getData() {
        return data;
    }

    public void setData(M data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
